<html>
<body>

<h1 style="text-align:center;">PHP Web Calculator</h1>
<h2 style="text-align:center;font-size:20px;">Project 1A CS143</h2>
<h3 style="text-align:center;font-size:20px;">Clement Fang 303458725</h3>
<BR>
<BR>
<BR>

<form action="calculator.php" method="GET">
<h4 style="text-align:center;font-size:20px;">Input Equation:<input type="text" name="equation" /></h4>
<h5 style="text-align:center;font-size:20px;"><input type="submit" value="Calculate!" /></h5>
</form>

<?php
if($_GET["equation"]){
	$noSpaceEquation = preg_replace("/\s+/","",$_GET["equation"]);
	$number = "(\-?\d+(([.]\d+)?))" ;//subpattern for a number
	$operation = "[+\/*-]";
	//FINAL valid regex equatiion Number (operand(number))repeated
	$validequation = "/^".$number."((".$operation.$number.")+)?$/";
	if(preg_match($validequation, $noSpaceEquation,$matches))
	{
		//a valid equation detected
		$zeroDetectString = "//";
		$nodoubleminusEquation = preg_replace("/--/","+",$matches[0]);
		eval("@\$answer =".$nodoubleminusEquation.";");
		if($answer == NULL)
		{
			print "<P align = center>" . "Invalid Input Expression divide by 0" . "</P>";
		}
		else
		{
			print "<P align = center>" . $matches[0]." = ".$answer . "</P>";
		}
	}
	else
	{
		print "<P align = center>" . "Invalid Input Expression"." ".$_GET["equation"] . "</P>";
	}
}
?>
</body>
</html>
